package com.cnblsp2.android.study.androidresources.raw;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.cnblsp2.android.study.androidresources.R;

public class RawResActivity extends Activity {

	private MediaPlayer mediaPlayer1 = null;
	private MediaPlayer mediaPlayer2 = null;

	private Button activity_rawres_play1_btn = null;
	private Button activity_rawres_play2_btn = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_rawres);

		// 直接根据声音文件的ID来创建MediaPlayer
		mediaPlayer1 = MediaPlayer.create(this, R.raw.m96384);
		// 获取该应用的AssetManager
		AssetManager am = getAssets();
		try {
			// 获取指定文件对应的AssetFileDescriptor
			AssetFileDescriptor afd = am.openFd("a96384.mp3");
			mediaPlayer2 = new MediaPlayer();
			// 使用MediaPlayer加载指定声音文件
			mediaPlayer2.setDataSource(afd.getFileDescriptor());
			mediaPlayer2.prepare();
		} catch (Exception e) {
			e.printStackTrace();
		}

		activity_rawres_play1_btn = (Button) this.findViewById(R.id.activity_rawres_play1_btn);
		activity_rawres_play2_btn = (Button) this.findViewById(R.id.activity_rawres_play2_btn);

		activity_rawres_play1_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mediaPlayer1.start();
			}
		});

		activity_rawres_play2_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mediaPlayer2.start();
			}
		});

	}

}
